home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************************
- FILE: ArrowCDEF Tester.c
- CREATED: March 17, 1994
- AUTHOR: David Hay
- DESCRIPTION: A simple program to test the ArrowCDEF.
-
- VERSION: 1.1
-
- Copyright © 1994 by David Hay
- *******************************************************************************/
-
- #include <ctype.h>
- #include "ArrowCDEF.h"
- #include "ArrowCDEFUtils.h"
-
- #define kBaseResID 128
- #define kMoveToFront (WindowPtr)-1L
-
- #define kBWArrow 128
- #define kColorArrow 129
-
- #define kEnterKey 0x03
- #define kReturnKey 0x0d
- #define kEscapeKey 0x1b
-
- #define kVisualDelay 8
- #define kScrollDelay 15
-
- #define kArrowCntl 2
- #define kOKOutline 3
- #define kTextItem 4
- #define kActiveItem 6
- #define kUseColorArrow 7
-
- #define kChecked 1
- #define kUnchecked 0
-
-
- /*******************************************************************************
- * FUNCTION: ToolBoxInit
- *
- * INPUT: None.
- * RETURNS: Nothing.
- *
- * DESCRIPTION: Initializes the Macintosh toolbox managers
- *******************************************************************************/
- static void ToolBoxInit( void )
- {
- InitGraf(&thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(nil);
- MaxApplZone();
- InitCursor();
- }
-
- /*******************************************************************************
- * FUNCTION: DialogFilter
- *
- * INPUT: DialogPtr theDialog -- The dialog to filter the events for
- * EventRecord* theEvent -- the event to filter.
- * short* itemHit -- The item hit returned here
- * RETURNS: Boolean -- Did the filter handle the event?
- *
- * DESCRIPTION: Handles routines intended for the given dialog. It intercepts
- * non-number entries into the text edit field and responds to
- * the Enter and Return keys.
- *******************************************************************************/
- static pascal Boolean DialogFilter( DialogPtr theDialog,
- EventRecord* theEvent,
- short* itemHit )
- {
- short itemType; /* The kind of dialog item returned from GetDItem */
- Handle itemHandle; /* Handle to a dialog item */
- Rect itemRect; /* Rectangle of a dialog item */
- char key; /* the key that was pressed */
- long finalTicks; /* number of ticks passed after a delay */
-
- switch( theEvent->what )
- {
- case keyDown:
- case autoKey:
- key = ( char ) ( ( theEvent->message ) & charCodeMask );
-
- if ( key == kReturnKey || key == kEnterKey )
- {
- /* Respond as if the OK button was pressed by hiliting the
- ** button for a visible amount of time
- **/
- GetDItem( theDialog, ok, &itemType, &itemHandle, &itemRect );
- HiliteControl( (ControlHandle)itemHandle, inButton );
- Delay( kVisualDelay, &finalTicks );
- HiliteControl( (ControlHandle)itemHandle, 0 );
- *itemHit = ok;
- return( true );
- }
- else
- {
- /* Only allow numbers to be entered and beep at
- ** the user if they enter something besides numbers
- **/
- if ( !isdigit( key ) && !iscntrl( key ) && key != '-')
- {
- SysBeep( 10 );
- *itemHit = kTextItem;
- return( true );
- }
- }
- break;
- }
- return( false ); /* Getting here means we didn't handle the event */
- }
-
- /*******************************************************************************
- * FUNCTION: ArrowActionProc
- *
- * INPUT: ControlHandle theControl -- The control affected
- * short part -- Part where the mouse is now
- * RETURNS: Nothing.
- *
- * DESCRIPTION: Action procedure for the arrow control. It simply updates the
- * value of the text edit field by incrementing or decrementing the
- * value depending upon which part of the arrow is being pressed.
- * The longer the user presses a part of the arrow, the faster the
- * values increment or decrement.
- *******************************************************************************/
- static pascal void ArrowActionProc( ControlHandle theControl, short part )
- {
- long theValue; /* The current value of the control */
- Str255 theString; /* the string in the edit text */
- DialogPtr theDialog; /* the dialog the control belongs to */
- Rect iRect; /* Rectangle of a dialog item */
- Handle iHandle; /* Handle to a dialog item */
- short iType; /* The type of a dialog item */
- static long delay = kScrollDelay; /* delay before an increment occurs */
- static long endTicks = 0; /* tick count when we will increment */
- static short oldDir = kPlainArrow; /* direction of the last time */
-
- if ( endTicks < TickCount() && part != kPlainArrow )
- {
- theValue = GetCtlValue( theControl );
- theDialog = (DialogPtr)(*theControl )->contrlOwner;
- GetDItem(theDialog, kTextItem, &iType, &iHandle, &iRect);
- if ( part == inUpButton ) /* pressed up arrow, increment theValue */
- {
- if ( theValue < GetCtlMax( theControl ) )
- theValue++;
- if ( oldDir != kUpArrow ) /* remember which part was pressed last */
- {
- oldDir = kUpArrow;
- delay = kScrollDelay;
- }
- }
- else if ( part == inDownButton ) /* pressed down arrow, decrement theValue */
- {
- if ( theValue > GetCtlMin( theControl ) )
- theValue--;
- if ( oldDir != kDownArrow ) /* remember which part was pressed last */
- {
- oldDir = kDownArrow;
- delay = kScrollDelay;
- }
- }
- SetCtlValue( theControl, theValue ); /* Set the new control value */
- NumToString( theValue, theString ); /* Convert the value to a string */
- SetIText(iHandle, theString); /* Set the text field to the new value */
- SelIText(theDialog, kTextItem, 0, 32767); /* hilite the text field */
-
- endTicks = TickCount() + delay; /* determine when we will allow a value change */
- if ( delay > 0 ) /* decrement the delay if we can */
- {
- delay--;
- }
- }
- }
-
- /*******************************************************************************
- * FUNCTION: PositionDialog
- *
- * INPUT: DialogPtr theDialog -- the dialog to position on the screen.
- * Boolean move -- should we actually move the dialog?
- * RETURNS: Point -- new top left corner of the dialog
- *
- * DESCRIPTION: Positions the given dialog centered horizontally on the screen
- * and in the upper third. The new location of the dialog is
- * returned. If move is true, then the dialog is moved to the new
- * position as well.
- *******************************************************************************/
- static Point PositionDialog( DialogPtr theDialog, Boolean move )
- {
- short width; /* width of theDialog */
- short height; /* height of theDialog */
- Point newLoc; /* new location of theDialog */
-
- width = theDialog->portRect.right - theDialog->portRect.left;
- height = theDialog->portRect.bottom - theDialog->portRect.top;
- newLoc.h = ( screenBits.bounds.right - width ) / 2;
- newLoc.v = ( screenBits.bounds.bottom - GetMBarHeight() - height ) / 3
- + GetMBarHeight();
-
- if ( move ) /* if we're supposed to move the dialog, do so */
- {
- MoveWindow( theDialog, newLoc.h, newLoc.v, true );
- }
- }
-
- /*******************************************************************************
- * FUNCTION: ButtonOutline
- *
- * INPUT: DialogPtr theDialog -- the dialog to draw in
- * short itemID -- item determining the button outline
- * RETURNS: Nothing.
- *
- * DESCRIPTION: Draws an outline for a button in the user item identified by
- * itemID.
- *******************************************************************************/
- static pascal void ButtonOutline( DialogPtr theDialog, short itemID )
- {
- short itemType; /* The type of the item in GetDItem */
- Handle theItem; /* handle to the item */
- Rect itemBox; /* the box containing the item */
- PenState savedPenState; /* old pen state resored on exit */
- GrafPtr origPort; /* the current port saved. restored on exit */
-
- GetPort( &origPort ); /* save the old port */
- SetPort( theDialog );
-
- GetDItem( theDialog, itemID, &itemType, &theItem, &itemBox );
-
- /* Draw the button outline */
- GetPenState( &savedPenState );
- PenNormal();
- PenSize( 3, 3 );
- FrameRoundRect( &itemBox, 16, 16 );
-
- /* Restore the old settings */
- SetPenState( &savedPenState );
- SetPort( origPort );
- }
-
- void main( void )
- {
- GrafPtr savePort; /* The old graphics port */
- DialogPtr theDialog; /* The dialog to display */
- short itemHit; /* Last item hit in the dialog */
- short itemType; /* The type of item */
- Handle itemHandle; /* handle to the item */
- Rect itemRect; /* an item's Rect */
- Str255 theString; /* misc. pascal string */
- long theValue; /* misc value. */
- Boolean done; /* is the program done yet? */
-
- ToolBoxInit();
-
- GetPort( &savePort );
-
- theDialog = GetNewDialog( kBaseResID, nil, kMoveToFront );
- if ( theDialog == NULL )
- {
- SysBeep( 10 );
- ExitToShell();
- }
-
- /* Center the dialog, select it, and make it
- ** the current graphics port.
- **/
- PositionDialog( theDialog, true );
- SelectWindow( theDialog );
- SetPort( theDialog );
-
- /* Give the arrow control an action procedure */
- GetDItem( theDialog, kArrowCntl, &itemType, &itemHandle, &itemRect );
- SetCtlAction( (ControlHandle) itemHandle, ArrowActionProc );
-
- /* Install a routine to draw the default button outline to a user item. */
- GetDItem( theDialog, kOKOutline, &itemType, &itemHandle, &itemRect );
- SetDItem( theDialog, kOKOutline, itemType, &ButtonOutline, &itemRect );
-
- /* Make the arrow activator initially active. */
- GetDItem( theDialog, kActiveItem, &itemType, &itemHandle, &itemRect );
- SetCtlValue( (ControlHandle) itemHandle, kChecked );
-
- /* Initially use the color arrow */
- GetDItem( theDialog, kUseColorArrow, &itemType, &itemHandle, &itemRect );
- SetCtlValue( (ControlHandle) itemHandle, kChecked );
-
- /* Done with initializations.
- ** Show the dialog and call ModalDialog until OK is pressed.
- **/
- ShowWindow( theDialog );
- done = false;
- while( !done )
- {
- ModalDialog( DialogFilter, &itemHit );
- switch ( itemHit )
- {
- case ok: /* OK button pressed. Exit the program. */
- done = true;
- break;
-
- case kTextItem: /* Text entered, get the new value */
- GetDItem( theDialog, kTextItem, &itemType, &itemHandle, &itemRect );
- GetIText( itemHandle, theString );
- StringToNum( theString, &theValue );
- GetDItem( theDialog, kArrowCntl, &itemType, &itemHandle, &itemRect );
- SetCtlValue( (ControlHandle)itemHandle, theValue );
- break;
-
- case kActiveItem: /* Change the active status of the arrow control */
- GetDItem( theDialog, kActiveItem, &itemType, &itemHandle, &itemRect );
- theValue = 1 - GetCtlValue( (ControlHandle) itemHandle );
- SetCtlValue( (ControlHandle) itemHandle, theValue );
- GetDItem( theDialog, kArrowCntl, &itemType, &itemHandle, &itemRect );
- HiliteControl( (ControlHandle) itemHandle, (theValue) ? 0 : 255 );
- break;
-
- case kUseColorArrow:
- GetDItem( theDialog, kUseColorArrow, &itemType, &itemHandle, &itemRect );
- theValue = 1 - GetCtlValue( (ControlHandle) itemHandle );
- SetCtlValue( (ControlHandle) itemHandle, theValue );
- GetDItem( theDialog, kArrowCntl, &itemType, &itemHandle, &itemRect );
- SetArrowAPIC( (ControlHandle) itemHandle,
- (theValue) ? kBWArrow : kColorArrow, true );
-
- default:
- break;
- }
- }
-
- DisposDialog( theDialog );
- SetPort( savePort );
- ExitToShell();
- }